<?php

namespace app\index\controller;

use app\index\service\BaseService;
use app\index\service\OrderService;
use think\Controller;
use think\Db;
use think\Validate;

class Asyntasks extends Controller {
	public function index() {
		echo 'Asyntasks';
	}
	/**
	 * [notice_distribute 查询所有待分发的通知]
	 * @DateTime 2018-09-14T12:28:59+0800
	 * @刘家腾
	 * @return   [type]                   [description]
	 */
	public function notice_distribute() {
		$list = Db::table('announcement')
			->where(['state' => 0, 'is_delete' => 0, 'is_first' => 1, 'is_synchro' => 0])
			->select();
		foreach ($list as $key => $value) {
			$this->notice_distribute_list($value);
			$update_time = date('Y-m-d H:i:s');
			Db::table('announcement')->where('id', $value['id'])->update(['is_synchro' => 1, 'update_time' => $update_time]);
		}

	}
	/**
	 * [notice_distribute_list 获取待发通知对应的产品 订单号]
	 * @DateTime 2018-09-14T12:30:23+0800
	 * @刘家腾
	 * @param    string                   $value [description]
	 * @return   [type]                          [description]
	 */
	public function notice_distribute_list($value = '') {
		if ($value['type'] == 1){
			//产品
			$product_info = BaseService::product_info($value['source_id']);
			if (!empty($product_info)) {
				$this->is_synchro($product_info['0'], $value);
			}
		} elseif ($value['type'] == 2) {
			//订单
			$orders_list_info = BaseService::orders_list_info($value['source_id']);
			if (!empty($orders_list_info)) {
				$orders_product_info = BaseService::product_info($orders_list_info['product_id']);
				if (!empty($orders_product_info)) {
					//$this->order_is_synchro($value, $orders_product_info['0']);
					$this->order_is_synchro($orders_product_info['0'], $value);
				}
			}
		}
	}
	/**
	 * [business_notice_add 按照商户ID添加通知]
	 * @DateTime 2018-09-14T15:00:50+0800
	 * @刘家腾
	 * @param    integer                  $business_id [description]
	 * @param    string                   $value       [description]
	 * @return   [type]                                [description]
	 */
	public function business_notice_add($product_data,$value = '') {
		$staff_list = BaseService::base_staff_list($product_data['business_id']);
		if($staff_list){
				foreach ($staff_list as $k => $v) {
						OrderService::notice_add($staff_list[$k]['user_id'], $value['action_type'], $value['type'], $staff_list[$k]['business_id'], $value['source_id'], $value['description'], 0, 0);
				}
		}
	}
	/**
	 * [is_synchro 产品类型通知 自有 同步]
	 * @DateTime 2018-09-14T15:03:14+0800
	 * @刘家腾
	 * @param    string                   $product_info [description]
	 * @param    string                   $value        [description]
	 * @return   boolean                                [description]
	 */
	public function is_synchro($product_info = '', $value = '') {
		if(in_array($value['action_type'],[2,3,4,5,6,7])){
			if ($product_info['is_synchro'] == 1) {
				OrderService::notice_add($product_info['business_id'], $value['action_type'], $value['type'], $product_info['business_id'], $value['source_id'], $value['description'], 0, 0);
				$this->business_notice_add($product_info,$value);
			}else{
					OrderService::notice_add($product_info['business_id'], $value['action_type'], $value['type'], $product_info['business_id'], $value['source_id'], $value['description'], 0, 0);
					$this->business_notice_add($product_info,$value);

					$synchro_product_listinfo = BaseService::synchro_product_info($product_info['id']);

					if ($synchro_product_listinfo != null) {
						foreach ($synchro_product_listinfo as $key => $v_list) {
							OrderService::notice_add($v_list['business_id'], $value['action_type'], $value['type'], $v_list['business_id'], $value['source_id'], $value['description'], 0, 0);
							$this->business_notice_add($v_list, $value);
						}
					}
				}
		}elseif(in_array($value['action_type'],[1])){
			OrderService::notice_add($product_info['business_id'], $value['action_type'], $value['type'], $product_info['business_id'], $value['source_id'], $value['description'], 0, 0);
			$this->business_notice_add($product_info,$value);
		}









	}
	/**
	 * [order_is_synchro 订单类型通知]
	 * @DateTime 2018-09-14T15:04:25+0800
	 * @刘家腾
	 * @param    string                   $product_info [description]
	 * @param    string                   $value        [description]
	 * @return   [type]                                 [description]
	 */
	public function order_is_synchro($product_info = '', $value = '') {
		if ($product_info['is_synchro'] == 1) {

			$this->business_notice_add($product_info, $value);
			$synchro_product_listinfo = BaseService::synchro_product_info($product_info['source_product_id']);
			foreach ($synchro_product_listinfo as $key => $v_list) {
				OrderService::notice_add($v_list['business_id'], $value['action_type'], $value['type'], $v_list['business_id'], $value['source_id'], $value['description'], 0, 0);
				$this->business_notice_add($v_list, $value);
			}

		} else {
			//自有
			OrderService::notice_add($product_info['business_id'], $value['action_type'], $value['type'], $product_info['business_id'], $value['source_id'], $value['description'], 0, 0);
			$this->business_notice_add($product_info, $value);

		}

	}

}
